package it.controllocredito.backend.persistence;

import java.util.ArrayList;
import java.util.List;

import it.controllocredito.backend.persistence.bean.Monitor;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.backend.persistence.tables.MonitorTable;
import it.controllocredito.backend.persistence.tables.RegistroChiamateTable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper
{
	
	private static final int	DATABASE_VERSION	= 1;
	private static final String	DATABASE_NAME		= "CONTROLLO_CREDITO_DB";
	
	public DatabaseHandler(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	// Crea le tabelle. Questo metodo va invocato solamente la prima volta che
	// viene lanciata l'app
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL(RegistroChiamateTable.getCreateQuery());
		// db.execSQL(MonitorTable.getCreateQuery());
	}
	
	public boolean addRecord(String tableName, ContentValues cv)
	{
		boolean recordAdded = false;
		SQLiteDatabase db = this.getWritableDatabase();
		try
		{
			db.insert(tableName, null, cv);
			db.close();
			recordAdded = true;
		} catch (Exception ex)
		{
			recordAdded = false;
			ex.printStackTrace();
		}
		return recordAdded;
	}
	
	// registro_chiamate
	
	public RegistroChiamate getRecordRegistroChiamate(int id)
	{
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(RegistroChiamateTable.getName(),
				RegistroChiamateTable.getColumns(),
				RegistroChiamateTable.getKeyColumn() + "=?", new String[]
				{ String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		
		RegistroChiamate registroChiamate = new RegistroChiamate();
		db.close();
		return registroChiamate;
	}
	
	public List<RegistroChiamate> getRecordsRegistroChiamate()
	{
		String sql = "SELECT  * FROM "
				+ RegistroChiamateTable.getName();
		
		return recordsRegistroChiamate(sql);
	}
	
	public List<RegistroChiamate> getRecordsRegistroChiamate(String sqlWehereCondition)
	{			
		String sql = "SELECT  * FROM "
				+ RegistroChiamateTable.getName() + " WHERE " + sqlWehereCondition;
		
		return recordsRegistroChiamate(sql);
	}
	
	private List<RegistroChiamate> recordsRegistroChiamate(String selectQuery)
	{
		List<RegistroChiamate> registroChiamateList = new ArrayList<RegistroChiamate>();
				
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		
		// looping through all rows and adding to list
		if (cursor.moveToFirst())
		{
			do
			{
				registroChiamateList.add(RegistroChiamateTable
						.getRecord(cursor));
			} while (cursor.moveToNext());
		}
		
		db.close();
		
		return registroChiamateList;
	}
	
	// monitor
	
	public Monitor getRecordMonitor(int id)
	{
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(MonitorTable.getName(),
				MonitorTable.getColumns(), MonitorTable.getKeyColumn() + "=?",
				new String[]
				{ String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		
		Monitor monitor = new Monitor();
		db.close();
		return monitor;
	}
	
	public List<Monitor> getAllRecordsMonitor()
	{
		List<Monitor> monitorList = new ArrayList<Monitor>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + MonitorTable.getName();
		
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		
		// looping through all rows and adding to list
		if (cursor.moveToFirst())
		{
			do
			{
				// Adding contact to list
				monitorList.add(MonitorTable.getRecord(cursor));
			} while (cursor.moveToNext());
		}
		
		db.close();
		// return contact list
		return monitorList;
	}
	
	public void dropTable(String tableName)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DROP TABLE IF EXISTS '" + tableName + "'");
		onCreate(db);
		
	}
	
	public boolean deleteAllRecords(String tableName)
	{
		boolean allRecordsDeleted = false;
		
		SQLiteDatabase db = this.getWritableDatabase();
		try
		{
			db.delete(tableName, null, null);
			db.close();
			allRecordsDeleted = true;
		} catch (Exception ex)
		{
			allRecordsDeleted = false;
			ex.printStackTrace();
		}
		return allRecordsDeleted;
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub
	}
}